package cn.tradewin.pmp.pages.security;

import java.util.ArrayList;
import java.util.List;

import org.apache.tapestry5.EventConstants;
import org.apache.tapestry5.ValueEncoder;
import org.apache.tapestry5.annotations.Log;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.beaneditor.BeanModel;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.BeanModelSource;
import org.slf4j.Logger;

import cn.tradewin.pmp.persist.model.Contract;
import cn.tradewin.pmp.persist.model.MCustomer;
import cn.tradewin.pmp.services.security.ContractService;


public class ContractManage {
	
	
	@Property
	private final ContractEncoder contractEncoder = new ContractEncoder();
	
	@Property
	private List<Contract> contracts;
	
	private Contract contract;
	
	private boolean inFormSubmission;

	private List<Contract> contractsSubmitted;

	@Inject
	private ContractService contractService;

	@Inject
	private BeanModelSource beanModelSource;
	
	@Property
	private BeanModel<Contract> beanModel;

	@Inject
	private Messages messages;
		
	@Inject
	Logger log;
		
	@Log
	void onActivate() {
		inFormSubmission = false;
	}
	
	@Log
	void setupRender() {
		beanModel = beanModelSource.createDisplayModel(Contract.class, messages);
		beanModel.add("detail", null);
		beanModel.include("selected", "contractNo", "assignDate", "partA", "contractName", "releaseDate", "estimateNo","contractAmount","tax", "detail");
		beanModel.get("detail").label("");
		beanModel.get("detail").sortable(false);
	}

	@Log
	@OnEvent(value = EventConstants.PREPARE_FOR_RENDER)
	void onPrepareForRender() {
		if (contracts == null) {
			contracts = contractService.getAllContracts();
		}
	}
	
	@OnEvent(value = EventConstants.PREPARE_FOR_SUBMIT)
	void prepareForSubmit() {
		inFormSubmission = true;
		contractsSubmitted = new ArrayList<Contract>();
	}
	
	
	/**
	 * do the delete tasks
	 */
	@Log
	void onSuccessFromListForm() {
		List<String> delContracts = new ArrayList<String>();
		if (contractsSubmitted != null) {
			log.debug("------------------- size = " + contractsSubmitted.size());
		}
		for (Contract m : contractsSubmitted) {
			if (m.isSelected()) {
				delContracts.add(m.getContractNo());
			}
		}
		if (delContracts.size() > 0) {
			contractService.DelContracts(delContracts);
		}
	}
	

	public boolean isNoData() {
		return (contracts == null || contracts.size() == 0) ? true : false;  
	}
	
	@Log
	void cleanupRender() {
		contracts = null;
	}

	public Contract getContract() {
		return contract;
	}

	public void setContract(Contract contract) {
		this.contract = contract;
		if (inFormSubmission) {
			this.contractsSubmitted.add(contract);
		}
	}
	
	private class ContractEncoder implements ValueEncoder<Contract> {
		public String toClient(Contract value) {
			return value.getContractNo();
		}
		public Contract toValue(String clientValue) {
			Contract m = new Contract();
			m.setContractNo(clientValue);
			return m;
		}
	}
}
